package com.mjk.common.web.getway.restful.interceptor.type.export.impl;


import com.mjk.common.base.error.BizException;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.interceptor.InterceptorChain;
import com.mjk.common.util.MapUtils;
import com.mjk.common.util.RandomUtil;
import com.mjk.common.web.getway.constns.GetWayError;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.annotation.BeeGetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import com.mjk.common.web.getway.restful.interceptor.type.export.consts.ExportConver;
import com.mjk.common.web.getway.restful.interceptor.type.export.consts.ExportProcessEnum;

import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 导出过程
 * 1 pre 准备阶段
	 progress 文件准备阶段 返回 进度条
	 down 进度条慢，文件下载阶段
   2 all 所有阶段，标识本次导出直接放回文件，不进行三次过程的调用
 * @author mjk
 *
 */
@BeeGetWayInterceptor(value=1,interceptorType= GetWayInterceptorType.EXPORT)
public class ExportProcessInterceptor implements GetWayInterceptor {
	/**
	 * 传入的导出文件名称
	 */
	public static final String FILE_NAME_KEY ="fileName";
	
	//private String FILE_UUID = "uuid";
	
	/**
	 * 下载转化map
	 */
	private final ConcurrentHashMap<String, ExportConver> converMap = new ConcurrentHashMap<>();
	@Override
	public BeeResponse interceptor(GetWayInterceptorReq request,
								   InterceptorChain<GetWayInterceptorReq, BeeResponse> next) {
		//获取下载文件的阶段
		String process = MapUtils.getString(request.getRestfulParams(), GetwayConsts.VERSION);
		BeeResponse response = new BeeResponse();
		response.setCode(GetWayError.SUC.getCode());
		String fileName =  MapUtils.getString(request.getParams(), FILE_NAME_KEY);		
		if(ExportProcessEnum.ALL.getProcess().contains(process)){
			String uuid = RandomUtil.getShortUuid();
			ExportConver conver = getConver(request);
			Map<String, Object>  map = request.getParams();
			map.putAll(request.getRestfulParams());
			conver.converSource(map);
			//因为下载的时候是读取文件 而不是使用这个二进制，所以不需要读取
			response.setData(conver.getFileByte());
			return response;
		}else{
			if(ExportProcessEnum.PRE.getProcess().equals(process)){
				//String uuid = RandomUtil.getShortUuid();
				ExportConver conver = getConver( request);
				Map<String, Object>  map = request.getParams();
				map.putAll(request.getRestfulParams());
				conver.converSource(map);
				response.setData(fileName);
				return response;
			}else if(ExportProcessEnum.PROCESS.getProcess().equals(process)){
				//获取UUID
				if(request.getParams().containsKey(fileName)){
					ExportConver conver = getConver( request);
					response.setData(conver.getProcess());
					return response;
				}else{
					throw new BizException("uuid is null");
				}
			}else if(ExportProcessEnum.DOWN.getProcess().equals(process)){
				if(request.getParams().containsKey(fileName)){
					ExportConver conver = getConver(request);
					response.setData(conver.getFileByte());
					return response;
				}else{
					throw new BizException("uuid is null");
				}
			}
		}
		return null;
	}
	
	public ExportConver getConver(GetWayInterceptorReq req){
		String fileName =  MapUtils.getString(req.getParams(), FILE_NAME_KEY);		
		if(converMap.containsKey(fileName)){
			return converMap.get(fileName);
		}else{
			File f = new File(req.getUploadPath());
			if(!f.exists()) {
				f.mkdir();
			}
			ExportConver conver = new ExportConver(req.getProtocal(), req.getUploadPath()+fileName);
			converMap.put(fileName, conver);
			return conver;
		}
	}	
}
